require 'episode_data_parser'
class Show < ActiveRecord::Base
  validates_presence_of :title, :on => :create, :message => "Shows must have a title"
  validates_uniqueness_of :title, :on => :create, :message => "This show has already been created"
  validates_presence_of :rss_url, :on => :create, :message => "Shows must have an rss link"
  validates_http_url :rss_url
  
  after_save :update_rss_feed
  
  has_many :episodes
  
  def update_rss_feed
    @rss_feed = ShowRss.new(self.rss_url)
  end
  
  def update_episodes
    @rss_feed.parse_episodes
    @rss_feed.episodes.each do |rss_episode|
      #create a new data parser
      episode_data_parser = EpisodeDataParser.new(rss_episode)        
      episode = Episode.new()
      episode.parse_rss(episode_data_parser)
      episode.save
    end
  end
end
